package com.yiyuzi.wiki.spine;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiyuzi.wiki.spine.dao.SpineDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hongxy
 * @date 2021/7/2 9:29
 */

@Service
public class SpineServiceImpl extends ServiceImpl<SpineDao, SpineEntity> implements SpineService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SpineServiceImpl.class);

    //    private String basePath = "D:/img/spine/assetbundle/all/spine";
    private String basePath = "/root/yiyuzi/benghuai2/spine";
    //    private String basePath = "D:/img/spine/assetbundle/all/spine/test";
    private List<Long> idList;

    /**
     * 数据储存在 TextAsset 里，图片存在 Texture2D 里，不创建实际意义的目录，而是根据skel、atlas对应的文件名创建虚拟目录。
     * 再根据atlas头几行的信息，将图片对应上目录，同时将图片们转为base64格式，存到数据库中。
     * 似乎根本不用存图片，直接根据url里的图片名找就行(...)
     * base64过长,先不转了
     */
    @Override
    public Boolean setSpine() {
        idList = new ArrayList<>();
        boolean text = setSpineText();
        boolean png = setSpinePng();
        // 删除多余的数据
        List<SpineEntity> spineEntityList = list();
        for (SpineEntity spineEntity : spineEntityList) {
            if (!idList.contains(spineEntity.getId())) {
                removeById(spineEntity.getId());
            }
        }
        return text && png;
    }

    @Override
    public Boolean setSpineText() {
        String path = basePath + "/TextAsset";
        File file = new File(path);
        File[] filesName = file.listFiles();
        if (filesName == null)
            return false;
        for (File file1 : filesName) {
            if (file1.isFile()) {
                String[] arr = file1.getName().split("[.]");
                SpineEntity father = baseMapper.selectOne(new QueryWrapper<SpineEntity>()
                        .eq("father_id", 0)
                        .eq("name", arr[0]));
                if (father == null) {
                    father = new SpineEntity();
                    father.setFatherId(0);
                    father.setName(arr[0]);
                    father.setPath(path);
                    baseMapper.insert(father);
                }
                idList.add(father.getId());
                SpineEntity spineEntity = baseMapper.selectOne(new QueryWrapper<SpineEntity>()
                        .eq("father_id", father.getId())
                        .eq("name", file1.getName()));
                if (spineEntity == null) {
                    spineEntity = new SpineEntity();
                    spineEntity.setFatherId(father.getId());
                    spineEntity.setName(file1.getName());
                    spineEntity.setPath(path + "/" + file1.getName());
                    spineEntity.setType(arr[1]);
                    baseMapper.insert(spineEntity);
                }
                idList.add(spineEntity.getId());
            }
        }
        return true;
    }

    @Override
    public Boolean setSpinePng() {
        String path = basePath + "/Texture2D";
        File file = new File(path);
        File[] filesName = file.listFiles();
        if (filesName == null)
            return false;
        for (File file1 : filesName) {
            if (file1.isFile()) {
                // 暂时不分表，反正也就几百条数据
                SpineEntity spineEntity = baseMapper.selectOne(new QueryWrapper<SpineEntity>()
                        .eq("name", file1.getName()));
                if (spineEntity == null) {
                    spineEntity = new SpineEntity();
                    spineEntity.setFatherId(-1);
                    spineEntity.setName(file1.getName());
                    spineEntity.setPath(path + "/" + file1.getName());
                    spineEntity.setType("png");
                    baseMapper.insert(spineEntity);
                }
                idList.add(spineEntity.getId());
            }
        }
        return true;
    }
}
